package cn.iocoder.yudao.module.call.controller.admin.location;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.call.controller.admin.speech.vo.SpeechPageReqVO;
import cn.iocoder.yudao.module.call.controller.admin.speech.vo.SpeechRespVO;
import cn.iocoder.yudao.module.call.convert.location.LocationConverter;
import cn.iocoder.yudao.module.call.dal.dataobject.speech.SpeechDO;
import cn.iocoder.yudao.module.call.service.vosnumber.VosNumberService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.module.call.controller.admin.location.vo.*;
import cn.iocoder.yudao.module.call.dal.dataobject.location.LocationDO;
import cn.iocoder.yudao.module.call.service.location.LocationService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Tag(name = "管理后台 - 归属地")
@RestController
@RequestMapping("/call/location")
@Validated
public class LocationController {

    @Resource
    private LocationService locationService;

    @Resource
    private VosNumberService vosNumberService;

    @PostMapping("/create")
    @Operation(summary = "创建归属地")
    @PreAuthorize("@ss.hasPermission('call:location:create')")
    public CommonResult<Long> createLocation(@Valid @RequestBody LocationSaveReqVO createReqVO) {
        return success(locationService.createLocation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新归属地")
    @PreAuthorize("@ss.hasPermission('call:location:update')")
    public CommonResult<Boolean> updateNumberPool(@Valid @RequestBody LocationSaveReqVO updateReqVO) {
        locationService.updateLocation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除归属地")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('call:location:delete')")
    public CommonResult<Boolean> deleteLocation(@RequestParam("id") Long id) {
        locationService.deleteLocation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得归属地")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<LocationRespVO> getLocation(@RequestParam("id") Long id) {
        LocationDO numberPool = locationService.getLocation(id);
        return success(BeanUtils.toBean(numberPool, LocationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得归属地分页")
    public CommonResult<PageResult<LocationRespVO>> getNumberPoolPage(@Valid LocationPageReqVO pageReqVO) {
        PageResult<LocationDO> pageResult = locationService.getLocationPage(pageReqVO);
        Map<Long, Long> numberCount = vosNumberService.getNumberCount(pageResult.getList().stream().map(LocationDO::getId).collect(Collectors.toList()));
        return success(LocationConverter.INSTANCE.convert0(pageResult, numberCount));
    }

    @GetMapping("/list")
    @Operation(summary = "获得归属地列表")
    @OperateLog(enable = false)
    public CommonResult<List<LocationRespVO>> getLocationList() {
        LocationPageReqVO pageReqVO = new LocationPageReqVO();
        pageReqVO.setPageNo(1);
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LocationDO> speech = locationService.getLocationPage(pageReqVO).getList();
        return success(BeanUtils.toBean(speech, LocationRespVO.class));
    }


}